package thread;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.SynchronousQueue;

import static print.Print.*;
class LiftOffRunner implements Runnable{

    private BlockingQueue<LiftOff> rockes;
    public LiftOffRunner(BlockingQueue<LiftOff> queue){
        this.rockes = queue;
    }

    public void add(LiftOff lo){
        try{
            rockes.put(lo);
        }catch (InterruptedException ex){
            print("InterruptedException during puting()");
        }
    }

    @Override
    public void run() {
        try{
            while (!Thread.interrupted()){
                LiftOff rocket = rockes.take();
                rocket.run();
            }
        }catch (InterruptedException ex){
            print("Working from task()");
        }
        print("Exiting LiftOffRunning");
    }
}

public class TestBlockingQueues {
    static void getKey(){
        try{
            new BufferedReader(new InputStreamReader(System.in)).readLine();
        }catch (java.io.IOException e){
            throw new RuntimeException(e);
        }
    }

    static void getKey(String message){
        print(message);
        getKey();
    }

    static void test(String msg,BlockingQueue<LiftOff> queue){
        print(msg);
        LiftOffRunner lor = new LiftOffRunner(queue);
        Thread t = new Thread(lor);
        t.start();
        for (int i = 0; i < 5;i ++){
            lor.add(new LiftOff(5));
        }
        getKey("Press 'Enter' + ("+msg+")");
        t.interrupt();
        print("Finished "+msg+" test");
    }

    public static void main(String[] args) {
        test("LinkingBlockingQueue",new LinkedBlockingDeque<LiftOff>());

        test("ArrayBlockingQueue",new ArrayBlockingQueue<LiftOff>(3));

        test("SynchronousQueue",new SynchronousQueue<LiftOff>());
    }
}
